<?php

require_once "dbconnect.php";

$city_id = $_POST['city_id'];
if( empty($city_id) ) $city_id = $_GET['city_id'];

$only_active = $_POST['is_active'];
if( empty($only_active) ) $only_active = $_GET['is_active'];

//echo "state_name = ".$state_name."<br>";

if( !empty($city_id) ){
	//get sub categories of the category with id = parent_id
	$sql = "SELECT ct.id AS id, c.id AS city_id, c.name as city_name, c.driver_fee, ct.name as car_type, ct.max_passengers, ct.image, ct.luggage_capacity, ct.position, ct.is_active, cc.est_price_per_distance_unit, cc.est_price_per_hour, cc.base_price, cc.first_x_mile_price, cc.most_popular, cc.available FROM city c, car_city cc, car_type ct WHERE cc.city = '".$city_id."' AND c.is_active = '1' AND available = '1' AND cc.car_type_id = ct.id AND c.id = cc.city";
}
else {
	//get all categories
	$sql = "SELECT ct.id AS id, c.id AS city_id, c.name as city_name, c.driver_fee, ct.name, ct.max_passengers, ct.image, ct.luggage_capacity, ct.position, ct.is_active, cc.est_price_per_distance_unit, cc.est_price_per_hour, cc.base_price, cc.first_x_mile_price, cc.most_popular, cc.available FROM city c, car_city cc, car_type ct WHERE c.is_active = '1' AND available = '1' AND cc.car_type_id = ct.id AND c.id = cc.city";
}
$res = $db->query($sql);

//For debug:
//echo "Total categories found: ".$res->num_rows;
//echo "<br>";

if( $res->num_rows == 0 ){
	echo json_encode(array("status"=>"no car found"));
	exit();
}
for( $i=0; $i<$res->num_rows; $i++ ){
	$car = array(
		"id"=>$res->rows[$i]['id'],
		"car_type"=>$res->rows[$i]['car_type'],
		"max_passengers"=>$res->rows[$i]['max_passengers'],
		"image"=>$res->rows[$i]['image'],
		"luggage_capacity"=>$res->rows[$i]['luggage_capacity'],
		"position"=>$res->rows[$i]['position'],
		"est_price_per_distance_unit"=>$res->rows[$i]['est_price_per_distance_unit'],
		"est_price_per_hour"=>$res->rows[$i]['est_price_per_hour'],
		"base_price"=>$res->rows[$i]['base_price'],
		"first_x_mile_price"=>$res->rows[$i]['first_x_mile_price'],
		"most_popular"=>$res->rows[$i]['most_popular'],
		"available"=>$res->rows[$i]['available'],
		"driver_fee"=>$res->rows[$i]['driver_fee'],
		"position"=>$res->rows[$i]['position'],
		"is_active"=>$res->rows[$i]['is_active']
	);
	$cars[] = $car;
}
echo json_encode(array("status"=>"ok","cars"=>$cars));

?>
